package name.ekr.assetguide.data;

import name.ekr.assetguide.domain.AssetsPrice;
import name.ekr.assetguide.domain.TimeBoundsException;

/**
 *Implements Template Method pattern [GoF]: getNextPrices()
 */
public class PriceIterator {

	private DataProvider dataProvider;
	private int cursor;


	public PriceIterator(DataProvider dataProvider) {
		this.dataProvider = dataProvider;
	}

	public AssetsPrice getNextPrices() throws TimeBoundsException {
		if(dataProvider.getData().size() == 0) throw new TimeBoundsException();
		
		AssetsPrice prices = null;
		try {
			prices = dataProvider.getData().get(cursor).getPrices();
			updateCursor();
		} catch (IndexOutOfBoundsException e) {
			throw new TimeBoundsException();
		}
		
		return prices;
	}


	public void reset() {
		cursor = 0;
	}
	
    protected void updateCursor()  {
        setCursor(getCursor()+1);
    }

	private void setCursor(int cursor) {
		this.cursor = cursor;
	}

	private int getCursor() {
		return this.cursor;
	}
}